package com.mdp.workflow.biz.re.ctrl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.core.entity.Result;
import com.mdp.core.query.QueryTools;
import com.mdp.core.utils.BaseUtils;
import com.mdp.core.utils.RequestUtils;
import com.mdp.qx.HasRole;
import com.mdp.safe.client.entity.User;
import com.mdp.safe.client.utils.LoginUtils;
import com.mdp.swagger.ApiEntityParams;
import com.mdp.workflow.biz.re.entity.Procdef;
import com.mdp.workflow.biz.re.service.FlowProcdefServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.flowable.engine.RepositoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value="/*/re/procdef")
@Api(tags={"act_re_procdef-操作接口"})
public class ProcdefController {
	
	static Logger logger =LoggerFactory.getLogger(ProcdefController.class);
	
	@Autowired
	private FlowProcdefServiceImpl procdefService;

	@Autowired
	RepositoryService repositoryService;

	@Value(value = "${mdp.platform-branch-id:platform-branch-001}")
	String platformBranchId;

	@ApiOperation( value = "act_re_procdef-查询列表",notes=" ")
	@ApiEntityParams(Procdef.class)
	@ApiResponses({
		@ApiResponse(code = 200,response=Procdef.class,message = "{tips:{isOk:true/false,msg:'成功/失败原因',tipscode:'错误码'},total:总记录数,data:[数据对象1,数据对象2,...]}")
	})
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public Result listProcdef(@ApiIgnore @RequestParam Map<String,Object> params){
			User user=LoginUtils.getCurrentUserInfo();
			RequestUtils.transformArray(params, "categoryIds");
			RequestUtils.transformArray(params, "tagIds");
			QueryWrapper<Procdef> qw = QueryTools.initQueryWrapper(Procdef.class , params).in("tenant_id_",platformBranchId,user.getBranchId());;
			IPage page=QueryTools.initPage(params);
			params.put("platformBranchId",platformBranchId);
			params.put("branchId",user.getBranchId());
			List<Map<String,Object>> datas = procdefService.selectListMapByWhere(page,qw,params);
			return Result.ok("query-ok","查询成功").setData(datas).setTotal(page.getTotal());

	}

	@ApiOperation( value = "查询一个",notes=" ")
	@RequestMapping(value="/queryById",method=RequestMethod.GET)
	public Result queryById( @RequestParam Map<String,Object> params){
		Assert.notNull(params.get("id"));
		 return Result.ok().setData(this.procdefService.getProcessDefinition((String) params.get("id")));
	}

	/**
	 * 根据主键挂起流程模型
	 */

	@HasRole(roles= {"flowAdmin","platformAdmin","branchAdmin"})
	@RequestMapping(value="/suspend")
	public Result suspend(@RequestBody Procdef procdef) {
			procdefService.suspend(procdef.getId());
			return Result.ok();
	}

	/**
	 * 根据主键激活模型
	 */

	@HasRole(roles= {"flowAdmin","platformAdmin","branchAdmin"})
	@RequestMapping(value="/activate")
	public Result activate(@RequestBody Procdef procdef) {
			procdefService.activate(procdef.getId());
			return Result.ok();
	}


}
